local PIGGY_COOLDOWN = TUNING.TOTAL_DAY_TIME * 3 -- 3天冷却（24分钟）

local PiggyHiManager = Class(function(self, inst)
    self.inst = inst
    self.timer = inst.components.timer

    self.cooldown = false
    self.exist = false

    -- 启动监听定时器完成事件
    inst:ListenForEvent("timerdone", function(_, data)
        if data and data.name == "piggy_cooldown" then
            self.cooldown = false
        end
    end)
end)

---------------------------------------------------------------
-- 是否可以召唤
---------------------------------------------------------------
function PiggyHiManager:CanSummon()
    return not self.exist and not self.cooldown
end

---------------------------------------------------------------
-- 记录嗨猪生成（烟花端调用）
---------------------------------------------------------------
function PiggyHiManager:OnPiggySummoned()
    self.exist = true
    self.cooldown = false
end

---------------------------------------------------------------
-- 记录嗨猪离开（嗨猪端调用）
---------------------------------------------------------------
function PiggyHiManager:OnPiggyLeave()
    self.exist = false
    self.cooldown = true
    if not self.timer:TimerExists("piggy_cooldown") then
        self.timer:StartTimer("piggy_cooldown", PIGGY_COOLDOWN)
    end
end

---------------------------------------------------------------
-- 存档与读档
---------------------------------------------------------------
function PiggyHiManager:OnSave()
    return {
        cooldown = self.cooldown,
        exist = self.exist,
    }
end

function PiggyHiManager:OnLoad(data)
    if data ~= nil then
        self.cooldown = data.cooldown or false
        self.exist = data.exist or false
    end
end

return PiggyHiManager
